#include <stdio.h>
#include <string.h>
#include <ctype.h>

// 定义学生结构体（与第一张图片完全一致）
typedef struct STUDENT {
    int ljt_sid;       // 学生唯一ID (1~1000)
    char ljt_sname[50]; // 学生姓名 (非空)
    int ljt_sage;      // 学生年龄 (1~120)
    char ljt_ssex;     // 学生性别 ('M'或'F')
} Student;

// 全局变量
Student stus[10];  // 学生数组（10个元素）
int count = 0;     // 当前学生数量

// 显示系统菜单（与第二张图片一致）
void displayMenu() {
    printf("\nWelcome to student management system\n");
    printf("1. Add student\n");
    printf("2. View student\n");
    printf("3. Delete student\n");
    printf("4. Update student\n");
    printf("5. Exit\n");
    printf("Enter your choice: ");
}

// 验证学生ID是否唯一
int isSidUnique(int sid) {
    for (int i = 0; i < count; i++) {
        if (stus[i].ljt_sid == sid) {
            return 0; // 重复
        }
    }
    return 1; // 唯一
}

// 添加学生
void addStudent() {
    if (count >= 10) {
        printf("Error: Maximum student capacity reached (10 students).\n");
        return;
    }

    Student s;
    
    // 输入学生ID (1-1000，唯一)
    do {
        printf("Enter student ID (1-1000): ");
        scanf("%d", &s.ljt_sid);
        if (s.ljt_sid < 1 || s.ljt_sid > 1000) {
            printf("Error: ID must be between 1 and 1000.\n");
        } else if (!isSidUnique(s.ljt_sid)) {
            printf("Error: ID already exists.\n");
        }
    } while (s.ljt_sid < 1 || s.ljt_sid > 1000 || !isSidUnique(s.ljt_sid)); 

    // 输入学生姓名 (非空)
    printf("Enter student name: ");
    getchar(); // 清除换行符
    fgets(s.ljt_sname, 50, stdin);
    s.ljt_sname[strcspn(s.ljt_sname, "\n")] = 0; // 移除换行符
    while (strlen(s.ljt_sname) == 0) {
        printf("Error: Name cannot be empty.\nEnter student name: ");
        fgets(s.ljt_sname, 50, stdin);
        s.ljt_sname[strcspn(s.ljt_sname, "\n")] = 0;
    }

    // 输入学生年龄 (1-120)
    do {
        printf("Enter student age (1-120): ");
        scanf("%d", &s.ljt_sage);
    } while (s.ljt_sage < 1 || s.ljt_sage > 120);

    // 输入学生性别 (M/F)
    do {
        printf("Enter student gender (M/F): ");
        scanf(" %c", &s.ljt_ssex); // 空格忽略空白字符
        s.ljt_ssex = toupper(s.ljt_ssex);
    } while (s.ljt_ssex != 'M' && s.ljt_ssex != 'F');

    // 添加到数组
    stus[count++] = s;
    printf("Student added successfully!\n");
}

// 查看所有学生
void viewStudents() {
    if (count == 0) {
        printf("No students in the system.\n");
        return;
    }

    printf("\n%-8s %-20s %-8s %s\n", "ID", "Name", "Age", "Gender");
    printf("------------------------------------------------\n");
    for (int i = 0; i < count; i++) {
        printf("%-8d %-20s %-8d %c\n", 
              stus[i].ljt_sid, stus[i].ljt_sname, stus[i].ljt_sage, stus[i].ljt_ssex);
    }
}

// 删除学生
void deleteStudent() {
    if (count == 0) {
        printf("No students in the system.\n");
        return;
    }

    int sid;
    printf("Enter student ID to delete: ");
    scanf("%d", &sid);

    for (int i = 0; i < count; i++) {
        if (stus[i].ljt_sid == sid) {
            // 将最后一个元素移动到删除位置
            stus[i] = stus[count - 1];
            count--;
            printf("Student deleted successfully!\n");
            return;
        }
    }
    printf("Error: Student with ID %d not found.\n", sid);
}

// 更新学生信息
void updateStudent() {
    if (count == 0) {
        printf("No students in the system.\n");
        return;
    }

    int sid;
    printf("Enter student ID to update: ");
    scanf("%d", &sid);

    for (int i = 0; i < count; i++) {
        if (stus[i].ljt_sid == sid) {
            // 更新姓名
            printf("Current name: %s\n", stus[i].ljt_sname);
            printf("Enter new name (press Enter to keep current): ");
            getchar(); // 清除换行符
            char newName[50];
            fgets(newName, 50, stdin);
            newName[strcspn(newName, "\n")] = 0;
            if (strlen(newName) > 0) {
                strcpy(stus[i].ljt_sname, newName);
            }

            // 更新年龄 (1-120)
            printf("Current age: %d\n", stus[i].ljt_sage);
            printf("Enter new age (1-120): ");
            int newAge;
            if (scanf("%d", &newAge) == 1 && newAge >= 1 && newAge <= 120) {
                stus[i].ljt_sage = newAge;
            }

            // 更新性别 (M/F)
            printf("Current gender: %c\n", stus[i].ljt_ssex);
            char newGender;
            do {
                printf("Enter new gender (M/F): ");
                scanf(" %c", &newGender);
                newGender = toupper(newGender);
            } while (newGender != 'M' && newGender != 'F' && newGender != '\n');
            if (newGender == 'M' || newGender == 'F') {
                stus[i].ljt_ssex = newGender;
            }

            printf("Student updated successfully!\n");
            return;
        }
    }
    printf("Error: Student with ID %d not found.\n", sid);
}

int main() {
    int choice;
    
    do {
        displayMenu();
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                addStudent();
                break;
            case 2:
                viewStudents();
                break;
            case 3:
                deleteStudent();
                break;
            case 4:
                updateStudent();
                break;
            case 5:
                printf("Exiting system...\n");
                break;
            default:
                printf("Invalid choice! Please enter 1-5.\n");
        }
    } while (choice != 5);

    return 0;
}